package wec_wallet.ebo.com.activity.register;

import android.os.Bundle;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import bassproject.ebo.com.ebobass.activity.MyBaseActivity;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.Lg;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.utils.Functions;
import wec_wallet.ebo.com.utils.URLs;

public class AgreementActivity extends MyBaseActivity {
//
    @BindView(R.id.actionbar_back)
    RelativeLayout actionbar_back;
    @BindView(R.id.tv_agreement)
    TextView tv_agreement;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agreement);
        ButterKnife.bind(this);
        initViews();
    }

    @Override
    public void initViews() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", 1);
        Functions.post(this, URLs.AGREEMENT, map, new Listener(), true, true);
    }

    @Override
    public void onClick(View view) {

    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onClick_Back();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    //返回键
    @OnClick(R.id.actionbar_back)
    void onClick_Back() {
        finish();
    }



    //注册用户协议
    class Listener implements IHttpPostListener {
        @Override
        public void onTimeOut() {

        }
        @Override
        public void responseSuccess(String result) {
            if (Ipost.isTimeOut(result)) {
                return;
            }
            Lg.d("注册用户协议 result=" + result);
            if (null != result) {
                try {
                    JSONObject resultObject = new JSONObject(result);
                    int error = resultObject.getInt("error");
                    String message = "";
                    if (resultObject.has("error_msg") && error != 0) {
                        message = resultObject.getString("error_msg");
                        IToast.show(message);
                    }

                    if (error == 0) {
                        String data = resultObject.getString("data");
                        tv_agreement.setText(Html.fromHtml(data));
                    }

                } catch (Exception e) {
                    // TODO: handle exception
                    Lg.d(getClass() + ",接口解析异常：\n" + e.toString());
                }
            }
        }
    }


}
